package com.lzb.provide;

import android.Manifest;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class SmsActivity extends AppCompatActivity {
    private Handler handler  = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0x123){
                Uri uri= Uri.parse("content://sms/sent");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                StringBuffer buffer = new StringBuffer();
                while (cursor.moveToNext()){
                    buffer.append("地址：").append(cursor.getString(cursor.getColumnIndex("address")));
                    buffer.append("收件人：").append(cursor.getString(cursor.getColumnIndex("person")));
                    buffer.append("内容：").append(cursor.getString(cursor.getColumnIndex("body")));
                    buffer.append("\n");
                }
                txt_body.setText(buffer.toString());
                Toast.makeText(SmsActivity.this, buffer.toString(), Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    });

    /** 内容提供者监听类 */
    private ContentObserver contentObserver = new ContentObserver(handler) {
        @Override
        public boolean deliverSelfNotifications() {
            return super.deliverSelfNotifications();
        }

        /**
         * ContentProvider的内容发生变化时，触发该方法
         * @param selfChange
         */
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            handler.sendEmptyMessage(0x123);
        }
        /**
         * ContentProvider的内容发生变化时，触发该方法  这个与上方法相比多个Uri
         * @param selfChange
         */
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            super.onChange(selfChange, uri);
//            handler.sendEmptyMessage(0x123);

        }
    };
    private TextView txt_body;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        txt_body = (TextView) findViewById(R.id.txt_body);
        Uri uri= Uri.parse("content://sms");
        getContentResolver().registerContentObserver(uri,true,contentObserver);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_SMS},0x234);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 0x234){
            if (grantResults[0] != 0){
                finish();
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        getContentResolver().unregisterContentObserver(contentObserver);
    }
}
